package com.example.pc.adapterdemo.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.example.pc.adapterdemo.R;
import com.example.pc.adapterdemo.adapter.TabLayoutAdapter;
import com.example.pc.adapterdemo.bean.TabLayoutBean;
import com.example.pc.adapterdemo.fragment.TabFragment;
import com.example.pc.adapterdemo.utils.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TabLayoutActivity extends AppCompatActivity {

    private List<TabLayoutBean> mList = new ArrayList<>();
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private TabLayoutAdapter tabLayoutAdapter;
    private TabLayoutBean tabLayoutBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tablayout);

        initView();
    }

    private void initView() {
        mTabLayout = findViewById(R.id.mTabLayout);
        mViewPager = findViewById(R.id.mViewPager);

        mViewPager.setOffscreenPageLimit(4);
        tabLayoutAdapter = new TabLayoutAdapter(getSupportFragmentManager(), mList);
        initData();
    }

    private void initData() {
        OkHttpUtils.get("http://h.playdota.cc/api/News/get_news_category_list", new OkHttpUtils.OnOkHttpCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onFailure(String msg) {

            }

            @Override
            public void onSuccessful(String json) {
                if (!TextUtils.isEmpty(json)) {
                    getJson(json);
                }
            }
        });
    }

    private void getJson(String json) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonData = (JSONObject) jsonArray.get(i);
                tabLayoutBean = new TabLayoutBean();
                tabLayoutBean.setName(jsonData.getString("name"));
                tabLayoutBean.setId(jsonData.getInt("id"));
                mList.add(tabLayoutBean);
            }

            for (int i = 0; i < mList.size(); i++) {
                int id = mList.get(i).getId();
                if (id > 4) {
                    mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
                }
                mList.get(i).setFragment(TabFragment.newInstance(id));
                mViewPager.setOffscreenPageLimit(mList.size() - 1);
            }

            tabLayoutAdapter.setDateSource(mList);
            tabLayoutAdapter.notifyDataSetChanged();
            mTabLayout.setupWithViewPager(mViewPager);
            mViewPager.setAdapter(tabLayoutAdapter);
        } catch (
                JSONException e) {
            e.printStackTrace();
        }
    }
}
